﻿$(function () {
    var sourceType = $("#hSourceType").val();
    if (sourceType == "0") {
        //$.MiniUEditorInit("AlterReason", 750, 105, true);  //迷你加载
        $.MiniUEditorInit($("#hid").val(), "ContractManagement/AlterOrderList/AlterReason", "AlterReason", 750, 105, true);  //迷你加载
    }
    else {
        //$.MiniUEditorInit("AlterReason", 750, 105, false);  //迷你加载
        $.MiniUEditorInit($("#hid").val(), "ContractManagement/AlterOrderList/AlterReason", "AlterReason", 750, 105, false);  //迷你加载
    }

    //当页面首次刷新的时候执行的事件
    initCombobox();
    var alterOrderID = $("#hAlterOrderID").val();
    initBillList(sourceType, alterOrderID);
    initTable(alterOrderID);

    var contractID = $("#ContractID").combobox('getValue');
    initTreeTable(contractID);

    if (sourceType == '0') {
        disableAll();
    }
});

function initCombobox() {
    //$("#ProjectID").combobox({
    //    editable: false, panelHeight: "auto",
    //    valueField: 'ID',
    //    textField: 'ProjectName',
    //    url: '/ProjectInfo/ProjectInfoList',
    //    onSelect: function (record) {
    //        $("#ContractID").combobox({
    //            editable: false, panelHeight: "auto",
    //            valueField: 'ID',
    //            textField: 'ContractName',
    //            url: '/ContractInfo/ContractInfoList?pid=' + record.ID,
    //            onSelect: function (contractRecord) {
    //                initTreeTable(contractRecord.ID);
    //                return;
    //            }
    //        });
    //    }
    //});

    //var projectID = $("#ProjectID").combobox('getValue');
    //if (projectID != "") {
    $("#ContractID").combobox({
        editable: false, panelHeight: "auto",
        valueField: 'ID',
        textField: 'ContractName',
        url: '/ContractInfo/ContractInfoList?correspondenceUnitCode=' + $("#hLastUpdateUserDepartment").val(),
        onLoadSuccess: function () {
            var data = $("#ContractID").combobox('getData');
            var sourceType = $("#hSourceType").val();
            if (data.length > 0 && sourceType == '1') {
                $("#ContractID").combobox('select', data[0]["ID"]);
            }
        },
        onSelect: function (record) {
            $("#hProjectID").val(record.ProjectID),
            $("#ProjectName").val(record.ProjectName),
            initTreeTable(record.ID);
            return;
        }
    });
    //}

    //$("#AlterType").combobox({
    //    editable: false, panelHeight: "auto",
    //    data: [
    //        { value: "一般", text: "一般" },
    //        { value: "较大", text: "较大" },
    //        { value: "重大", text: "重大" }
    //    ]
    //});
}

function initBillList(sourceType, alterOrderID) {
    var processType = $("#hProcessType").val();
    var associatedNodeCode = $("#hAssociatedNodeCode").val();
    var lastUpdateUserDepartment = $("#hLastUpdateUserDepartment").val();
    var lastUpdateUser = $("#hLastUpdateUser").val();
    if (sourceType == "0") {
        $('#myBillList').datagrid({
            url: '/AlterOrder/AlterOrderBillList?sourceType=' + sourceType + '&processType=' + processType + '&associatedNodeCode=' + associatedNodeCode + '&lastUpdateUserDepartment=' + lastUpdateUserDepartment + '&lastUpdateUser=' + lastUpdateUser + '&alterOrderID=' + alterOrderID,
            nowrap: true,
            autoRowHeight: false,
            striped: true,
            rownumbers: true,
            remoteSort: false,
            idField: 'ID',
            singleSelect: true,
            columns: [[
                        { title: 'ID', field: 'ID', hidden: true },
                        { title: 'PID', field: 'PID', hidden: true },
                        { title: 'AlterOrderBillID', field: 'AlterOrderBillID', hidden: true },
                        { title: '清单号', field: 'BillCode', width: 80, sortable: true },
                        { title: '清单名称', field: 'BillName', width: 150, sortable: true },
                        { title: '单位', field: 'Unit', width: 50, sortable: true },
                        { title: '原数量', field: 'OriginalCount', width: 90, sortable: true },
                        { title: '变更后数量', field: 'AfterAlterCount', width: 90, sortable: true },
                        { title: '增减数量(+ -)', field: 'AlterCount', width: 100, sortable: true },
                        { title: '单价', field: 'Price', width: 60, sortable: true },
                        { title: '增减金额(+ -)', field: 'AlterMoney', width: 100, sortable: true },
                        { title: '备注', field: 'Remark', width: 150, sortable: true, editor: 'text' }
            ]]
        });
    }
    else {
        $('#myBillList').datagrid({
            url: '/AlterOrder/AlterOrderBillList?sourceType=' + sourceType + '&processType=' + processType + '&associatedNodeCode=' + associatedNodeCode + '&lastUpdateUserDepartment=' + lastUpdateUserDepartment + '&lastUpdateUser=' + lastUpdateUser + '&alterOrderID=' + alterOrderID,
            nowrap: true,
            autoRowHeight: false,
            striped: true,
            rownumbers: true,
            remoteSort: false,
            idField: 'ID',
            singleSelect: true,
            toolbar: '#toolBar',
            columns: [[
                        { title: 'ID', field: 'ID', hidden: true },
                        { title: 'PID', field: 'PID', hidden: true },
                        { title: 'AlterOrderBillID', field: 'AlterOrderBillID', hidden: true },
                        {
                            title: '操作', field: 'DeleteAction', width: 70, align: 'center',
                            formatter: function (value, row, index) {
                                return '<a href="#" onclick="deleterow(this)">删除</a> ';
                            }
                        },
                        { title: '清单号', field: 'BillCode', width: 80, sortable: true },
                        { title: '清单名称', field: 'BillName', width: 150, sortable: true },
                        { title: '单位', field: 'Unit', width: 50, sortable: true },
                        { title: '原数量', field: 'OriginalCount', width: 90, sortable: true, editor: { type: 'numberbox', options: { min: -99999999.999, max: 99999999.999, precision: 3 } } },
                        { title: '变更后数量', field: 'AfterAlterCount', width: 90, sortable: true, editor: { type: 'numberbox', options: { min: -99999999.999, max: 99999999.999, precision: 3 } } },
                        { title: '增减数量(+ -)', field: 'AlterCount', width: 100, sortable: true },
                        { title: '单价', field: 'Price', width: 60, sortable: true },
                        { title: '增减金额(+ -)', field: 'AlterMoney', width: 100, sortable: true },
                        { title: '备注', field: 'Remark', width: 150, sortable: true, editor: 'text' }
            ]],
            onBeforeEdit: function (currentIndex, row) {
                row.editing = true;
                $('#myBillList').datagrid('refreshRow', currentIndex);
            },
            onSelect: function (currentIndex, row) {
                $("#hOldOriginalCount").val(row.OriginalCount);
                $("#hOldAfterAlterCount").val(row.AfterAlterCount);
            },
            onAfterEdit: function (currentIndex, row) {
                var currentAlterMoney = Math.round((row.AfterAlterCount - row.OriginalCount) * row.Price)
                if (currentAlterMoney > 999999999999) {
                    row.OriginalCount = $("#hOldOriginalCount").val();
                    row.AfterAlterCount = $("#hOldAfterAlterCount").val();
                    $.EasyuiSlide('增减金额：999999999999');
                }
                else {
                    var alterBillList = $('#myBillList').datagrid('getData').rows;
                    var AlterMoney = 0;
                    for (var i = 0; i < alterBillList.length; i++) {
                        if (i != currentIndex) {
                            AlterMoney += alterBillList[i].AlterMoney;
                        }
                        else {
                            AlterMoney += currentAlterMoney;
                        }
                    }
                    AlterMoney = Math.round(AlterMoney);

                    if (AlterMoney > 999999999999) {
                        row.OriginalCount = $("#hOldOriginalCount").val();
                        row.AfterAlterCount = $("#hOldAfterAlterCount").val();
                        $.EasyuiSlide('增减金额：999999999999');
                    }
                    else {
                        row.AlterCount = (row.AfterAlterCount - row.OriginalCount).toFixed(3);
                        row.AlterMoney = currentAlterMoney;
                        $("#AlterMoney").val(AlterMoney);
                        if (AlterMoney <= 50000) {
                            $("#AlterType").val("一般");
                        }
                        else if (AlterMoney <= 100000) {
                            $("#AlterType").val("较大");
                        }
                        else {
                            $("#AlterType").val("重大");
                        }
                    }
                }
                AddEvent(currentIndex);
            },
            onLoadSuccess: function (data) {
                var alterBillList = data.rows;
                for (var i = 0; i < alterBillList.length; i++) {
                    AddEvent(i);
                }
            }
        });
    }
}

//暂时没用
function RevocationBillValue(row) {
    row.AlterCount = (row.AfterAlterCount - row.OriginalCount).toFixed(3);
    row.OriginalMoney = Math.round(row.OriginalCount * row.OriginalPrice);
    row.PresentMoney = Math.round(row.AfterAlterCount * row.PresentPrice);
    row.AlterMoney = Math.round(row.PresentMoney - row.OriginalMoney);

    var alterBillList = $('#myBillList').datagrid('getData').rows;
    var AlterMoney = 0;
    for (var i = 0; i < alterBillList.length; i++) {
        AlterMoney += alterBillList[i].AlterMoney;
    }
    AlterMoney = Math.round(AlterMoney);

    $("#AlterMoney").val(AlterMoney);
    if (AlterMoney <= 50000) {
        $("#AlterType").val("一般");
    }
    else if (AlterMoney <= 100000) {
        $("#AlterType").val("较大");
    }
    else {
        $("#AlterType").val("重大");
    }
}

function initTable(alterOrderID) {
    $('#myDataGrid').datagrid({
        url: '/ProcessedTask/ProcessedTaskList?recordID=' + alterOrderID,
        fitColumns: true,
        nowrap: true,
        autoRowHeight: false,
        striped: true,
        collapsible: true,
        pagination: true,
        pageSize: 20,
        rownumbers: true,
        remoteSort: false,
        idField: 'ID',
        singleSelect: true,
        columns: [[
                    { title: '节点名称', field: 'CurrentProcessNodeName', width: 200, sortable: true },
                    { title: '审批意见', field: 'ApprovalOpinion', width: 300, sortable: true },
                    { title: '处理人部门', field: 'CurrentProcessPersonDepartmentName', width: 100, sortable: true },
                    { title: '处理人', field: 'CurrentProcessPersonName', width: 100, sortable: true },
                    { title: '处理时间', field: 'ProcessTimeFormat', width: 100, sortable: true }
        ]]
    });

    $('#myCirculatedDataGrid').datagrid({
        url: '/FlowProcessMessage/GetFlowProcessMessageListForReceipt?recordID=' + alterOrderID + "&processType=1",
        iconCls: 'icon-bricks',
        nowrap: true,
        autoRowHeight: false,
        striped: true,
        collapsible: true,
        pagination: true,
        pageSize: 20,
        remoteSort: false,
        idField: 'ID',
        singleSelect: true,
        columns: [[
                    { title: '描述', field: 'DisplayText', width: 600, sortable: true },
                    { title: '时间', field: 'AssociatedDate', width: 200, sortable: true },
        ]]
    });

    $('#myReceiptDataGrid').datagrid({
        url: '/FlowProcessMessage/GetFlowProcessMessageListForReceipt?recordID=' + alterOrderID + "&processType=2",
        iconCls: 'icon-bricks',
        nowrap: true,
        autoRowHeight: false,
        striped: true,
        collapsible: true,
        pagination: true,
        pageSize: 20,
        remoteSort: false,
        idField: 'ID',
        singleSelect: true,
        columns: [[
                    { title: '描述', field: 'DisplayText', width: 600, sortable: true },
                    { title: '时间', field: 'AssociatedDate', width: 200, sortable: true },
        ]]
    });
}

function initTreeTable(contractID) {
    var sourceType = $("#hSourceType").val();
    if (sourceType == "0") {
        $('#myTreeGrid').treegrid({   //定位到Table标签，Table标签的ID是myTreeGrid
            url: "/ContractInfo/GetContractBillInfoListForAlterOrder?contractID=" + contractID,   //指向后台的Action来获取当前用户的信息的Json格式的数据
            nowrap: true,
            autoRowHeight: false,
            singleSelect: true,
            striped: true,
            pagination: false,
            rownumbers: true,
            remoteSort: false,
            idField: 'ID',
            treeField: 'BillCode',
            parentField: 'PID',
            columns: [[
                { title: '清单编号', field: 'BillCode', width: 130 },
                { title: '清单名称', field: 'BillName', width: 140 },
                { title: '单价', field: 'Price', width: 80 }
            ]]
        });
    }
    else {
        $('#myTreeGrid').treegrid({   //定位到Table标签，Table标签的ID是myTreeGrid
            url: "/ContractInfo/GetContractBillInfoListForAlterOrder?contractID=" + contractID,   //指向后台的Action来获取当前用户的信息的Json格式的数据
            nowrap: true,
            autoRowHeight: false,
            singleSelect: true,
            striped: true,
            pagination: false,
            rownumbers: true,
            remoteSort: false,
            idField: 'ID',
            treeField: 'BillCode',
            parentField: 'PID',
            columns: [[
                { title: '清单编号', field: 'BillCode', width: 130 },
                { title: '清单名称', field: 'BillName', width: 140 },
                { title: '单价', field: 'Price', width: 80 }
            ]],
            onDblClickRow: function (row) {
                if (row.NodeType == 0) return;
                var alterBillList = $('#myBillList').datagrid('getData').rows;

                if (!IsExists(alterBillList, row.BillCode)) {
                    $('#myBillList').datagrid('insertRow', {
                        index: alterBillList.length,
                        row: {
                            ID: NewGuid(),
                            AlterOrderBillID: NewGuid(),
                            PID: $("#hAlterOrderID").val(),
                            BillCode: row.BillCode,
                            BillName: row.BillName,
                            Unit: row.Unit,
                            AlterCount: 0,
                            Price: row.Price,
                            AlterMoney: 0,
                            Remark: row.Remark
                        }
                    });

                    var currentIndex = alterBillList.length - 1;
                    $('#myBillList').datagrid('unselectAll');
                    $('#myBillList').datagrid('selectRow', currentIndex);
                    AddEvent(currentIndex);

                    var alterBillList = $('#myBillList').datagrid('getData').rows;
                    var AlterMoney = 0;
                    for (var i = 0; i < alterBillList.length; i++) {
                        AlterMoney += alterBillList[i].AlterMoney;
                    }
                    $("#AlterMoney").val(AlterMoney);
                    if (AlterMoney <= 50000) {
                        $("#AlterType").val("一般");
                    }
                    else if (AlterMoney <= 100000) {
                        $("#AlterType").val("较大");
                    }
                    else {
                        $("#AlterType").val("重大");
                    }
                }
            }
        });
    }
}

function disableAll() {
    $('#ContractID').combobox('disable');
    $('#AlterOrderCode').attr("disabled", "disabled");
    $('#AlterOrderName').attr("disabled", "disabled");
    $('#PileNo').attr("disabled", "disabled");
    $('#AlterPosition').attr("disabled", "disabled");
    $('#ApprovalNumber').attr("disabled", "disabled");
    $('#ApprovalDate').attr("disabled","true");
    $('btnSelectAlter').linkbutton('disable');
    $('#OriginalChartName').attr("disabled", "disabled");
    $('#AlterChartName').attr("disabled", "disabled");
    $('#AlterMoney').attr("disabled", "disabled");
    $('#OriginalChartNo').attr("disabled", "disabled");
    $('#AlterChartNo').attr("disabled", "disabled");
    $('#AlterDate').attr("disabled", "disabled");
}

function deleterow(target) {
    $.messager.confirm("提示", "你确定要删除吗?", function (r) {
        if (r) {
            var tr = $(target).closest('tr.datagrid-row');
            var index = parseInt(tr.attr('datagrid-row-index'));
            $('#myBillList').datagrid('deleteRow', index);
            //$.messager.alert('操作提示', '删除成功!', 'info');
            //删除之后，给所有的行加上change事件
            $('#myBillList').datagrid('acceptChanges');
            var alterBillList = $('#myBillList').datagrid('getData').rows;
            var AlterMoney = 0;
            for (var i = 0; i < alterBillList.length; i++) {
                AddEvent(i);
                AlterMoney += alterBillList[i].AlterMoney;
            }
            $("#AlterMoney").val(AlterMoney);
            if (AlterMoney <= 50000) {
                $("#AlterType").val("一般");
            }
            else if (AlterMoney <= 100000) {
                $("#AlterType").val("较大");
            }
            else {
                $("#AlterType").val("重大");
            }
        }
    });
}

function AddEvent(currentIndex) {
    $('#myBillList').datagrid('beginEdit', currentIndex);
    var editorOriginalCount = $('#myBillList').datagrid('getEditor', { index: currentIndex, field: 'OriginalCount' });
    var editorCount = $('#myBillList').datagrid('getEditor', { index: currentIndex, field: 'AfterAlterCount' });

    editorOriginalCount.target.bind('change', function () {
        var tr = $(this).closest('tr.datagrid-row');
        var index = parseInt(tr.attr('datagrid-row-index'));
        $('#myBillList').datagrid('endEdit', index);
    });
    editorOriginalCount.target.bind('click', function () {
        var tr = $(this).closest('tr.datagrid-row');
        var index = parseInt(tr.attr('datagrid-row-index'));
        $('#myBillList').datagrid('selectRow', index);
    });

    editorCount.target.bind('change', function () {
        var tr = $(this).closest('tr.datagrid-row');
        var index = parseInt(tr.attr('datagrid-row-index'));
        $('#myBillList').datagrid('endEdit', index);
    });
    editorCount.target.bind('click', function () {
        var tr = $(this).closest('tr.datagrid-row');
        var index = parseInt(tr.attr('datagrid-row-index'));
        $('#myBillList').datagrid('selectRow', index);
    });
}

function SelectAlter() {
    var projectID = $("#ProjectID").combobox('getValue');
    var contractID = $("#ContractID").combobox('getValue');
    if (projectID == "" || contractID == "") {
        $.EasyuiSlide("所属项目、所属标段不能为空！");
    }
    else {
        showMyWindow("#alterOrderWindow", "变更申请", "/Alter/ShowFinishAlter?projectID=" + projectID + "&contractID=" + contractID, 500, 280, true, false);
    }
}

function SetSelectAlterReturnValue(idArray, codeArray) {
    var container = document.getElementById('refCode');
    if (container.hasChildNodes()) {
        $("#refCode").empty()
    }

    var idStr = '';
    var refCodeStr = '';
    for (var i = 0; i < idArray.length; i++) {
        if (i == idArray.length - 1) {
            refCodeStr += "<a href='#' onclick=PopAlter('" + idArray[i] + "')>" + codeArray[i] + "</a>";
            idStr += idArray[i];
        }
        else {
            refCodeStr += "<a href='#' onclick=PopAlter('" + idArray[i] + "')>" + codeArray[i] + "</a>、";
            idStr += idArray[i] + ',';
        }
    }
    $("#hRefAlterID").val(idStr);
    $(refCodeStr).appendTo($("#refCode"));

    var alterOrderID = $("#hAlterOrderID").val();
    $.post("/Alter/GetAlterBillListByAlterIDStr?alterIDStr=" + idStr + "&alterOrderID=" + alterOrderID, function (res) {
        var alterBillList = $('#myBillList').datagrid('getData').rows;
        for (var i = 0; i < res.length; i++) {
            if (!IsExists(alterBillList, res[i].BillCode)) {
                $('#myBillList').datagrid('insertRow', {
                    index: alterBillList.length + i,
                    row: {
                        ID: NewGuid(),
                        AlterOrderBillID: NewGuid(),
                        PID: res[i].PID,
                        BillCode: res[i].BillCode,
                        BillName: res[i].BillName,
                        Unit: res[i].Unit,
                        OriginalCount: res[i].OriginalCount,
                        AfterAlterCount: res[i].AfterAlterCount,
                        AlterCount: res[i].AlterCount,
                        Price: res[i].PresentPrice,
                        AlterMoney: res[i].AlterMoney,
                        Remark: res[i].Remark
                    }
                });

                var currentIndex = alterBillList.length - 1;
                $('#myBillList').datagrid('unselectAll');
                $('#myBillList').datagrid('selectRow', currentIndex);
                AddEvent(currentIndex);
            }
        }

        var alterBillList = $('#myBillList').datagrid('getData').rows;
        var AlterMoney = 0;
        for (var i = 0; i < alterBillList.length; i++) {
            AlterMoney += alterBillList[i].AlterMoney;
        }
        $("#AlterMoney").val(AlterMoney);
    });
}

function ValidateAddData(BillCode)
{
    var alterBillList1 = $('#myBillList').datagrid('getData').rows;
    $('#myTreeGrid').treegrid('selectAll');
    var alterBillList2 = $('#myTreeGrid').treegrid('getSelections');
    $('#myTreeGrid').treegrid('unselectAll');
    if (IsExists(alterBillList1, BillCode) || IsExists(alterBillList2, BillCode))
    {
        return false;
    }
    return true;
}

function SetAddReturnValue(BillCode, BillName, Unit, Price, OriginalCount, AfterAlterCount,AlterCount,AlterMoney, Remark)
{
    var alterBillList = $('#myBillList').datagrid('getData').rows;
    $('#myBillList').datagrid('insertRow', {
        index: alterBillList.length,
        row: {
            ID: NewGuid(),
            AlterOrderBillID: NewGuid(),
            PID: $("#hAlterOrderID").val(),
            BillCode: BillCode,
            BillName: BillName,
            Unit: Unit,
            OriginalCount: OriginalCount,
            AfterAlterCount: AfterAlterCount,
            AlterCount: AlterCount,
            Price: Price,
            AlterMoney: AlterMoney,
            Remark: Remark
        }
    });

    var currentIndex = alterBillList.length - 1;
    $('#myBillList').datagrid('unselectAll');
    $('#myBillList').datagrid('selectRow', currentIndex);
    AddEvent(currentIndex);

    var alterBillList = $('#myBillList').datagrid('getData').rows;
    var AlterMoney = 0;
    for (var i = 0; i < alterBillList.length; i++) {
        AlterMoney += alterBillList[i].AlterMoney;
    }
    $("#AlterMoney").val(AlterMoney);
    if (AlterMoney <= 50000) {
        $("#AlterType").val("一般");
    }
    else if (AlterMoney <= 100000) {
        $("#AlterType").val("较大");
    }
    else {
        $("#AlterType").val("重大");
    }
}

function IsExists(alterBillList, billCode) {
    var isExists = false;
    for (var i = 0; i < alterBillList.length; i++) {
        if (billCode == alterBillList[i].BillCode) {
            isExists = true;
            break;
        }
    }
    return isExists;
}

function PopAlter(alterID) {
    showMyWindow("#alterOrderWindow", "", "/Alter/Alter?MenuCode=Alter&sourceType=0&processType=1&sourceID=&alterID=" + alterID, 1000, 500, true, true, true);
}

var AlterOrder =
    {
        Save: function () {
            var contractID = $("#ContractID").combobox('getValue');
            var alterOrderCode = $("#AlterOrderCode").val();
            var alterOrderName = $("#AlterOrderName").val();
            var alterDate = $("#AlterDate").val();
            if (contractID == "" || alterOrderCode == "" || alterOrderName == "" || alterDate == "") {
                $.EasyuiSlide("所属标段、变更令编码、变更令名称和增减工期不能为空！");
                return false;
            }
            SaveData();
        },

        Circulation: function () {
            var flowCode = $("#hFlowCode").val();
            var id = $("#hAlterOrderID").val();
            var handleState = $("#hHandleState").val();
            var nodeCode = $("#hAssociatedNodeCode").val();
            var processUserDepartment = $("#hLastUpdateUserDepartment").val();
            var processUser = $("#hLastUpdateUser").val();

            $.post("/AlterOrder/CanCirculation?flowCode=" + flowCode + "&recordID=" + id + "&currentFlowNodeCode=" + nodeCode + "&processUserDepartment=" + processUserDepartment, function (res) {
                if (res == "") {
                    showMyWindow("#alterOrderWindow", "流程审批", "/Flow/CirculationFlow?flowCode=" + flowCode + "&recordID=" + id + "&handleState=" + handleState + "&currentFlowNodeCode=" + nodeCode + "&processUserDepartment=" + processUserDepartment + "&processUser=" + processUser, 500, 250, true, false);
                }
                else {
                    $.EasyuiSlide(res);
                }
            });
        },

        Withdraw: function () {
            var withdrawNodeCode = $("#hProcessdOrCirculatedNodeCode").val();
            $('#frmAlterOrder').form('submit', {
                url: '/AlterOrder/Withdraw?withdrawNodeCode=' + withdrawNodeCode,
                onSubmit: function () {

                },
                success: function (data) {
                    window.parent.CloseWindow('撤回成功！');
                },
                fail: function (data) {
                    $.EasyuiSlide(data);
                }
            });
        },

        Back: function () {
            var flowCode = $("#hFlowCode").val();
            var id = $("#hAlterOrderID").val();
            var nodeCode = $("#hAssociatedNodeCode").val();
            showMyWindow("#alterOrderWindow", "流程回退", "/Flow/BackFlow?flowCode=" + flowCode + "&recordID=" + id + "&flowNodeCode=" + nodeCode, 550, 400, true, false);
        },

        Cancel: function () {
            var flowCode = $("#hFlowCode").val();
            var id = $("#hAlterOrderID").val();
            $.post("/AlterOrder/Cancel?flowCode=" + flowCode + "&id=" + id, function (res) {
                if (res == "Success") {
                    window.parent.CloseWindow('终止成功！');
                }
                else {
                    $.EasyuiSlide(res);
                }
            });
        },

        Circulated: function () {
            var flowCode = $("#hFlowCode").val();
            var id = $("#hAlterOrderID").val();
            var nodeCode = $("#hProcessdOrCirculatedNodeCode").val();
            var department = $("#hProcessdOrCirculatedUserDepartment").val();
            showMyWindow("#alterOrderWindow", "流程传阅", "/Flow/CirculatedFlow?flowCode=" + flowCode + "&recordID=" + id + "&nodeCode=" + nodeCode + "&department=" + department, 400, 400, true, false);
        },

        Receipt: function () {
            var flowCode = $("#hFlowCode").val();
            var messageID = $("#hSourceID").val();
            showMyWindow("#alterOrderWindow", "流程已阅", "/Flow/ReceiptFlow?flowCode=" + flowCode + "&messageID=" + messageID, 500, 250, true, false);
        },

        Reminders: function () {
            var flowCode = $("#hFlowCode").val();
            var id = $("#hAlterOrderID").val();
            $.post("/AlterOrder/Reminders?flowCode=" + flowCode + "&id=" + id, function (res) {
                if (res == "Success") {
                    window.parent.CloseWindow('催办成功！');
                }
                else {
                    $.EasyuiSlide(res);
                }
            });
        },

        Add: function () {
            var contractID = $("#ContractID").combobox('getValue');
            if (contractID == "") {
                $.EasyuiSlide("所属标段不能为空！");
            }
            else {
                showMyWindow("#alterOrderWindow", "新增变更令清单", "/AlterOrder/AddAterOrderBill?contractID=" + contractID, 620, 240, true, false);
            }

        }
    }

function CloseSelf() {
    var handleState = $("#hHandleState").val();
    window.parent.cancelPopWindow(handleState);
}

function SaveData() {
    var alterOrderBillList = $('#myBillList').datagrid('getData').rows;
    var alterOrderBillListJson = JSON.stringify(alterOrderBillList);
    $('#hAlterOrderBillListJson').val(alterOrderBillListJson);
    $('#frmAlterOrder').form('submit', {
        url: '/AlterOrder/Save',
        onSubmit: function () {

        },
        success: function (data) {
            $('#btnCirculation').linkbutton('enable');
            $('#btnCirculated').linkbutton('enable');
            $.EasyuiSlide("保存成功！");
        },
        fail: function (data) {
            $.EasyuiSlide(data);
        }
    });
}

function PreviewProcessGraph(title, index) {
    if (index == 1) {
        var flowCode = $("#hFlowCode").val();
        var id = $("#hAlterOrderID").val();
        $.post("/AlterOrder/GenerateGraph?flowCode=" + flowCode + "&id=" + id, function (res) {
            var container = document.getElementById('graphContainer');
            if (container.hasChildNodes()) {
                $("#graphContainer").empty()
            }
            PreviewGraph(container, res);
        }
        )
    }
}

//关闭弹出窗口，按条件刷新主界面
//0：取消，1：发送，2：回退，3：传阅，4：已阅
function CloseWindow(closeType, data) {
    colseMyWindow("#alterOrderWindow");
    if (closeType != 0) {
        window.parent.CloseWindow(data);
    }
}

function cancelPopWindow() {
    colseMyWindow("#alterOrderWindow");
}
